using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

namespace PrintScreen
{
    public class EventActionArgs : EventArgs
    {

        public EventActionArgs(string name)
            : base()
        {
            Name = name;
        }
        public string Name;
    }

    public abstract class ActionToDo
    {
        public ActionToDo(ImageInfo i)
        {
            image = i;
        }
        protected ImageInfo image;
        public virtual string Action
        {
            get
            {
                return actionData;
            }
            set
            {
                actionData= value;
            }
        }
        protected string actionData;
        public string Name;
        public event EventHandler<EventArgs> AfterAction;
        private void OnAfterAction()
        {
            if (AfterAction != null)
                AfterAction(this, new EventActionArgs(this.Name));
        }
       
        public virtual void DoAction()
        {
            OnAfterAction();

            if (NextAction != null)
            {
                NextAction.image = this.image;
                NextAction.DoAction();
            }

        }
        public string Serialize()
        {
            StringBuilder sb = new StringBuilder();
            EncodingStringWriter sw = new EncodingStringWriter(sb);
            sw._encoding = System.Text.Encoding.Default;
            XmlTextWriter tw = new XmlTextWriter(sw);
            tw.Formatting = Formatting.Indented;
            Ser.Serialize(tw, this);
            return sb.ToString();
        }
        XmlSerializer m_ser;
        private XmlSerializer Ser
        {
            get
            {
                if (m_ser == null)
                    m_ser = new XmlSerializer(this.GetType());

                return m_ser;
            }
        }
        public ActionToDo NextAction;
    }

    public abstract class ActionToDo_Fixed :ActionToDo
    {
        public ActionToDo_Fixed(ImageInfo i)
            : base(i)
        {

        }
        public override string Action
        {
            get
            {
                return base.Action;
            }
            set
            {
                //do not set another action
            }
        }
    }
    public class ActionToDo_Run : ActionToDo
    {
        protected string Arguments;
        
        public ActionToDo_Run(ImageInfo i)
            : base(i)
        {

        }
        public override void DoAction()
        {
            if (!string.IsNullOrEmpty(Action))
            {

                System.Diagnostics.Process.Start(Action,Arguments);
            }

            base.DoAction();
        }
    }


    public class ActionToDo_None : ActionToDo_Fixed
    {
        public ActionToDo_None(ImageInfo i)
            : base(i)
        {
            actionData = "<NONE>";
        }
    }

    public class ActionToDo_RunFiles : ActionToDo_Run
    {
        public ActionToDo_RunFiles(ImageInfo i)
            : base(i)
        {
            
        }
        public override void DoAction()
        {
            ActionTodo_Save a = new ActionTodo_Save(base.image);
            a.DoAction();
            System.Diagnostics.Process.Start(a.FileNameFull);
            System.Diagnostics.Process.Start(a.FileNameActive);            
            base.DoAction();

        }
    }
    public class ActionToDo_RunDir : ActionToDo_Run
    {
        public ActionToDo_RunDir(ImageInfo i)
            : base(i)
        {
            ActionTodo_Save a = new ActionTodo_Save(base.image);
            a.DoAction();
            actionData = "explorer";            
            Arguments = "/select," + a.FileNameFull;
        }
    }
    
}
